package com.winter.Controller;

import com.jgoodies.common.base.Strings;
import com.winter.model.IAtta;
import com.winter.service.UploadingService;
import com.winter.util.Constants;
import com.winter.util.Result;
import com.winter.util.UUIDUitls;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("v1/up")
public class UploadingController {

    @Autowired
    private UploadingService uploadingService;

    @RequestMapping("file")
    public Result pCPhotoUpload(String type, String needsThumb, HttpServletRequest request) throws Exception {
        try {
            CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(request.getSession()
                                                                                             .getServletContext());
            StringBuffer pictureUrls = new StringBuffer();
            List<String> imagePath = new ArrayList<>();
            if (multipartResolver.isMultipart(request)) {
                MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request;
                Iterator<String> iter = multiRequest.getFileNames();
                if (iter.hasNext()) {
                    while (iter.hasNext()) {
                        MultipartFile file = multiRequest.getFile((String) iter.next());
                        //文件名称
                        String fileName = file.getOriginalFilename();
                        if (!fileName.equals("")) {
                            //获得文件后缀名称
                            String prefix = fileName.substring(fileName.lastIndexOf(".") + 1);
                            //图片文件放的位置文件夹目录
                            String path = "";
                            // 自定义的文件名称
                            String trueFileName = UUIDUitls.uuid() + "." + prefix;
                            String acesP = "";
                            // 设置存放图片文件的路径
                            if (type.equals("1")) {
                                path = System.getProperty("user.dir") + System.getProperty("file.separator") + "att_p2p" + System.getProperty("file.separator") + "upload" + System.getProperty("file.separator") + "app-avatar" + System.getProperty("file.separator");
                                acesP = System.getProperty("file.separator") + "upload" + System.getProperty("file.separator") + "app-avatar" + System.getProperty("file.separator") + trueFileName;
                            } else if (type.equals("2")) {
                                path = System.getProperty("user.dir") + System.getProperty("file.separator") + "att_p2p" + System.getProperty("file.separator") + "upload" + System.getProperty("file.separator") + "card" + System.getProperty("file.separator");
                                acesP = System.getProperty("file.separator") + "upload" + System.getProperty("file.separator") + "card" + System.getProperty("file.separator") + trueFileName;
                            } else if (type.equals("3")) {
                                path = System.getProperty("user.dir") + System.getProperty("file.separator") + "att_p2p" + System.getProperty("file.separator") + "upload" + System.getProperty("file.separator") + "org" + System.getProperty("file.separator");
                                acesP = System.getProperty("file.separator") + "upload" + System.getProperty("file.separator") + "org" + System.getProperty("file.separator") + trueFileName;
                            }
                            System.out.println(path);
                            String pathFile = path + trueFileName;
                            //转为标准文件盘符格式// "

                            final File localFile = new File(path);
                            //将文件复制到指定文件夹下面
                            try {
                                if (!localFile.exists()) {
                                    localFile.mkdirs();
                                }
                                file.transferTo(new File(pathFile));
                            } catch (Exception e) {
                            }
                            imagePath.add(pathFile);
                            StringBuffer sb = new StringBuffer();
                            sb.append(acesP);
                            sb.append(",");
                            pictureUrls.append(sb);
                        }
                    }
                    String purl = "";
                    if (Strings.isEmpty(pictureUrls.toString())) {
                        return Result.failure(703, "没有图片路径");
                    } else {
                        Map<String, String> retMap = new HashMap<>();
                        purl = pictureUrls.substring(0, pictureUrls.length() - 1);
                        retMap.put("purl", purl);
                        return Result.success(purl);
                    }
                } else {
                    return Result.failure(701, "未发现图片");
                }
            } else {
                return Result.failure(700, "未发现图片");
            }
        } catch (Exception e) {
            return Result.failure(400, "参数有误");
        }
    }


    /**
     * 上传附件
     */
    @PostMapping("accessory")
    public Result accessory(@RequestBody IAtta iAtta) {
        return uploadingService.accessory(iAtta);
    }


}
